<?php

class Control_ContactController extends Zend_Controller_Action {

    public function init() {
        $this->_helper->layout->setLayout('admin');
        $this->yesButton = 'Yes'; //напис на кнопці Так під час видалення
        $this->noButton = 'No'; //напис на кнопці Ні під час видалення
        $this->contactDb = new Control_Model_Table_Contact();
        $this->view->controller = $this->getRequest()->getControllerName();
        $this->view->action = $this->getRequest()->getActionName();
    }

    public function indexAction() {
        $this->_helper->redirector('edit');
    }

    public function addAction() {
        $form = new Control_Model_Form_Contact();
        $form->submit->setLabel('Add');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $type = $form->getValue('type');
                $title = $form->getValue('title');
                $link = $form->getValue('link');
                $mail = $form->getValue('mail');
                $this->contactDb->addData($type, $title, $link, $mail);
                $this->_helper->redirector('edit');
            } else {
                $form->populate($formData);
            }
        }
    }

    public function editAction() {
        $this->view->PortfolioData = $this->contactDb->getAllData();
        $form = new Control_Model_Form_Contact();
        $form->submit->setLabel('Update');
        $this->view->form = $form;
        $delparam = $this->_getParam('del', 0);
        $id = (int) $this->_getParam('id', 0);
        ///--------------------Google link form----------------//
        $googleForm = new Control_Model_Form_GoogleLink();
        $googleForm->submit->setLabel('Update link');
        $googleLink = $this->contactDb->getGoogle();
        $googleForm->populate($googleLink[0]);
        $formToUpdate = $this->getRequest()->getPost();

     //           Zend_Debug::dump($formToUpdate['forma']);exit();
        ///---------------------Google link form posted-------------------------//
        if ($this->getRequest()->isPost() && (isset($formToUpdate['googleLink']))) { //Якщо відправлена форма з сторінки едіт
            $formContentData = $this->getRequest()->getPost();
            if ($googleForm->isValid($formContentData)) {
                $link = $googleForm->getValue('link');
                $this->contactDb->setGoogleLink($link);
                $this->_helper->redirector('edit');
            } else {
                $googleForm->populate($formContentData);
            }
        }
        ///---------------------------------------------------------------------//

        if ($this->getRequest()->isPost() && ($delparam != 1)) { //Якщо відправлена форма без видалення
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('id');
                $type = $form->getValue('type');
                $title = $form->getValue('title');
                $link = $form->getValue('link');
                $mail = $form->getValue('mail');
                $this->contactDb->updateData($id, $type, $title, $link, $mail);
                $this->_helper->redirector('edit');
            } else {
                $form->populate($formData);
            }
        } elseif ($this->getRequest()->isPost() && ($delparam == 1)) { //Якщо відправлена форма з видаленням
            if ($this->getRequest()->isPost()) {
                $del = $this->getRequest()->getPost('del');
                if ($del == $this->yesButton) {
                    $id = $this->getRequest()->getPost('id');
                    $this->contactDb->deleteData($id);
                } else {
                    $this->_helper->redirector('edit');
                }
                $this->_helper->redirector('edit');
            }
        } else {//Виведення сторінки едіт
            $this->view->formGoogle = $googleForm;
//            $id = (int) $this->_getParam('id', 0);
            $del = (int) $this->_getParam('del', 0);
            $this->view->get_id = $id;
            if ($id > 0 && $del == 0) {
                $formdata = $this->contactDb->getDataID($id);
                if (isset($formdata)) {
                    $form->populate($formdata);
                } else {
                    $this->view->get_id = 0;
                };
            } elseif ($id > 0 && $del == 1) {
                $this->view->get_del = 1;
                $this->view->yesButton = $this->yesButton;
                $this->view->noButton = $this->noButton;
                $this->view->NameToDelete = $this->contactDb->getDataID($id);
            } elseif ($id == 0) {
                $this->view->get_id = $id;
            } else {
                $this->view->get_id = 0;
                echo "<span style='color: red;'>Error id: $id<br></span>";
            }
        }
    }

}

